package com.acane.instagram.feature.postDetail

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument


const val POST_DETAIL_ROUTE = "post_detail"
const val POST_DETAIL_ROUTE_WITH_POST_ID = "post_id"
const val COMMENT_ID_PARAM = "commentId" // 新增参数常量

fun NavGraphBuilder.postDetailScreen(
    navController: NavHostController,
    toProfile: (String) -> Unit,
) {
    composable(
        route = "$POST_DETAIL_ROUTE/{$POST_DETAIL_ROUTE_WITH_POST_ID}?$COMMENT_ID_PARAM={$COMMENT_ID_PARAM}",
        arguments = listOf(
            navArgument(POST_DETAIL_ROUTE_WITH_POST_ID) {
                type = NavType.StringType
            },
            navArgument(COMMENT_ID_PARAM) {
                type = NavType.StringType
                nullable = true
                defaultValue = null
            }
        )
    )
    { backStackEntry ->
        val commentId = backStackEntry.arguments?.getString(COMMENT_ID_PARAM)
        PostDetailRoute(
            navController = navController,
            toProfile = toProfile,
            //savedStateHandle = backStackEntry.savedStateHandle,
            initialCommentId = commentId // 新增参数传递
        )
    }
}

// 扩展函数支持两种跳转方式
fun NavController.navigateToPostDetail(
    postId: String,
    commentId: String? = null
) {
    val route = buildString {
        append("$POST_DETAIL_ROUTE/$postId")
        commentId?.let { append("?$COMMENT_ID_PARAM=$it") }
    }
    navigate(route)
}